package com.example.service.impl;

import com.example.mapper.problemMapp;
import com.example.pojo.*;
import com.example.service.problemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * ClassName:problemlmpl
 * Description:
 *
 * @Author 赵永峰
 * @Create 2024/6/13 17:31
 * @Version 1.0
 */
@Service//注解：表示把这个注入到了ben中。
@Slf4j//可以输出注解
public class problemlmpl implements problemService {
    @Autowired
    private problemMapp problemMapp;

    @Value("${file.upload-dir}")
    private String saveDir;
    public void insertOne(problemPojo problemPojo){
        //组合修改时间，创建时间
        problemPojo.setUpdateTime(LocalDateTime.now());
        problemPojo.setEstablishTime(LocalDateTime.now());
        problemMapp.insertOne(problemPojo);
    }
    //修改问题
    public void problemAddEdit(problemPojo problemPojo){
        problemPojo.setUpdateTime(LocalDateTime.now());
        problemMapp.problemAddEdit(problemPojo);
    }
    //加载时，查询所有的问题
    public List<problemPojo> problemAllList(){
        List<problemPojo> problemPojoList=problemMapp.problemAllList();
        return problemPojoList;
    }
    //根据ID，向数据库中写入代码。
    public void upDataField(Map<String, Object> parameters){

        Integer idUse = (Integer) parameters.get("idUse");
        String field = (String) parameters.get("field");
        String val = (String) parameters.get("val");
        problemMapp.upDataField(idUse,field,val);
    }
    //根据ID，向数据库中写入代码(数字类）
    public void upDataFieldNmb(Map<String, Object> parameters){

        Integer idUse = (Integer) parameters.get("idUse");
        String field = (String) parameters.get("field");
        Integer val = (Integer) parameters.get("val");
        LocalDateTime uptime=LocalDateTime.now();
        problemMapp.upDataFieldNmb(idUse,field,val,uptime);
    }
    //根据用户的筛选条件进行筛选
    public List<problemPojo> problemAllListType(Map<String, Object> parameters){

        int typeUse=(int) parameters.get("intValue");
        String str1=(String) parameters.get("str1");
        String str2=(String) parameters.get("str2");
        String str3=(String) parameters.get("str3");
        if (typeUse==1){
            List<problemPojo> problemPojoList=problemMapp.problemAllListType1();
            return problemPojoList;
        }else if (typeUse==2){
            List<problemPojo> problemPojoList=problemMapp.problemAllList();
            return problemPojoList;
        }
        return null;
    }
    //按照录入日期查询根据不同的条件，有是不是我自己的，是不是未结题的，然后是那天录入的
    public List<problemPojo> problemInputDateQuery(Map<String, Object> parameters){
        String isMy=(String) parameters.get("isMy");
        String isEnd=(String) parameters.get("isEnd");
        String isDate=(String) parameters.get("isDate");
        //查询我自己的下的不同类型，查询的是我自己的情况
        if (isMy!="all"){
            //筛选自己的+未结题的+按照录入日期筛选
            if (isEnd=="noEnd"){
                //筛选自己的+未结题的+按照录入日期筛选
                List<problemPojo> problemPojoList=problemMapp.problemInputDateQueryIsMy(isMy,"否",isDate);
                return problemPojoList;
            }
            //筛选自己的+已结题的+按照录入日期筛选
            else if (isEnd=="end"){
                List<problemPojo> problemPojoList=problemMapp.problemInputDateQueryIsMy(isMy,"是",isDate);
                return problemPojoList;
            }
            //筛选自己的+全部+按照录入日期筛选
            else if (isEnd=="all"){
                List<problemPojo> problemPojoList=problemMapp.problemInputDateQueryIsMyAll(isMy,isDate);
                return problemPojoList;
            }
        }
        else if (isMy=="all"){
            if (isEnd=="noEnd"){
                //筛选全部的+未结题+按照录入日期筛选
                List<problemPojo> problemPojoList=problemMapp.problemInputDateQueryAll("否",isDate);
                return problemPojoList;
            }
            //筛选全部的+已结题+按照录入日期筛选
            else if (isEnd=="end"){
                List<problemPojo> problemPojoList=problemMapp.problemInputDateQueryAll("是",isDate);
                return problemPojoList;
            }
            else if (isEnd=="all"){
                List<problemPojo> problemPojoList=problemMapp.problemInputDateQueryIsDate(isDate);
            }
        }
        return null;
    }
    //上传问题描述位置照片
    public String describeAnnex(MultipartFile file, Integer id, String describeAnnex){
        try {
            // 记录文件名，并返回
            String fileNameAll = null;
            if (!file.isEmpty()) {
                // 生成文件的唯一名称以避免冲突
                String fileName = UUID.randomUUID() + "&&" + file.getOriginalFilename();
                String filePath = saveDir + fileName;
                File dest = new File(filePath);
                // 将文件保存到服务器上的指定位置
                file.transferTo(dest);
                // 将以前的文件名称与新的文件名称组合
                fileNameAll = describeAnnex + fileName + "|*|";
                // 将数据写入到数据库中
                LocalDateTime dateTime = LocalDateTime.now();
                problemMapp.upDataOneField(id, "describeAnnex", fileNameAll, dateTime);
            }
            // 如果文件不为空且未发生异常，返回文件名列表
            return fileNameAll;
        } catch (IOException e) {
            // 捕获异常并返回错误消息
            throw new RuntimeException("文件保存失败：" + e.getMessage());
        }
    }
    //删除文件的函数
    public String oilPictureRemove(Map<String, Object> parameters){
        Integer id=(Integer) parameters.get("id");
        String fieldName=(String) parameters.get("fieldName");
        String filedValue=(String) parameters.get("filedValue");
        String pictureName=(String) parameters.get("pictureName");
        //删除文件
        String filePath =saveDir+pictureName; // 替换为你的文件路径
        // 使用 Paths 类的静态方法 get 来根据给定的文件路径字符串 filePath 创建一个 Path 对象
        Path path = Paths.get(filePath);
        try {
            // 使用 Files 类的 delete 方法删除指定的文件
            Files.delete(path);
            //修改数据库
            // 将数据写入到数据库中
            LocalDateTime dateTime = LocalDateTime.now();
            problemMapp.upDataOneField(id, fieldName, filedValue, dateTime);
            return filedValue;
        } catch (IOException e) {
            System.err.println("删除文件时出错: " + e.getMessage());
            return "删除文件时出错";
        }
    }
    //从新问题库中，查询某一个字段，等于多个值的数据(是查询字符串）
    public List<problemDataPojo> inquiryProblemOneFindSeveralVal(Map<String, Object> map){
        String filedName=(String) map.get("filedName");
        String firstFiledVal=(String) map.get("firstFiledVal");
        String secondFiledVal=(String) map.get("secondFiledVal");
        String threeFiledVal=(String) map.get("threeFiledVal");
        String fourthFiledVal=(String) map.get("fourthFiledVal");
        String fifthFiledVal=(String) map.get("fifthFiledVal");
        List<problemDataPojo> pojos=problemMapp.inquiryProblemOneFindSeveralVal(filedName,firstFiledVal,secondFiledVal,threeFiledVal,fourthFiledVal,fifthFiledVal);
        return pojos;
    }
    //修改新问题库中的某一行，修改所有的字段
    public void editOneAll(problemDataPojo pojo){
        problemMapp.editOneAll(pojo);
    }
    //批量对问题索赔单的数据进行插入
    public void insertWarrantyList(List<warrantyClaimsPojo> warrantyClaimsPojoList){
        for (int i = 0; i < warrantyClaimsPojoList.size(); i++) {
            warrantyClaimsPojoList.get(i).setEstablishTime(LocalDateTime.now());
            warrantyClaimsPojoList.get(i).setUpdateTime(LocalDateTime.now());
        }
        problemMapp.insertWarrantyList(warrantyClaimsPojoList);
    }
    public void insertWarrantyListCBU(List<warrantyClaimsCBUPojo> warrantyClaimsCBUPojoList){
        for (int i = 0; i < warrantyClaimsCBUPojoList.size(); i++) {
            warrantyClaimsCBUPojoList.get(i).setCreate_time(LocalDateTime.now());
            warrantyClaimsCBUPojoList.get(i).setUpdate_time(LocalDateTime.now());
        }
        problemMapp.insertWarrantyListCBU(warrantyClaimsCBUPojoList);
    }
    public void insertWarrantyListKD(List<warrantyClaimsKDPojo> warrantyClaimsKDPojoList){
        for (int i = 0; i < warrantyClaimsKDPojoList.size(); i++) {
            warrantyClaimsKDPojoList.get(i).setCreate_time(LocalDateTime.now());
            warrantyClaimsKDPojoList.get(i).setUpdate_time(LocalDateTime.now());
        }
        problemMapp.insertWarrantyListKD(warrantyClaimsKDPojoList);
    }

    //查询出所有的索赔信息
    public List<warrantyClaimsPojo> inquiryWarrantyClaimAll(){
        List<warrantyClaimsPojo> warrantyClaimsPojoList=problemMapp.inquiryWarrantyClaimAll();
        return warrantyClaimsPojoList;
    }
    public List<warrantyClaimsCBUPojo> inquiryWarrantyClaimCBUAll(){
        List<warrantyClaimsCBUPojo> warrantyClaimsCBUPojoList=problemMapp.inquiryWarrantyClaimCBUAll();
        return warrantyClaimsCBUPojoList;
    }
    public List<warrantyClaimsKDPojo> inquiryWarrantyClaimKDAll(){
        List<warrantyClaimsKDPojo> warrantyClaimsKDPojoList=problemMapp.inquiryWarrantyClaimKDAll();
        return warrantyClaimsKDPojoList;
    }

    //辅助函数，对售后索赔单的字段进行修改，根据id，修改多个字段，字段名称需要按照‘/’隔开，val也是一样的，并且需要和字段对应
    public void editWarrantyClaimField(Map<String, Object> map){
        Integer id=(Integer) map.get("id") ;
        String filedName=(String) map.get("filedName");
        String filedVal=(String) map.get("filedVal");
        String filedType=(String) map.get("filedType");
        String[] filedNameList=filedName.split("/");
        String[] filedValList=filedVal.split(Pattern.quote("|&&|"));
        String[] filedTypeList=filedType.split("/");
        for (int i = 0; i < filedNameList.length; i++) {
            //确认类型
            if (filedTypeList[i].equals("str")){
                problemMapp.editWarrantyClaimOneField(id, filedNameList[i],filedValList[i]);
            }
            else if (filedTypeList[i].equals("int")){
                problemMapp.editWarrantyClaimOneFieldInt(id, filedNameList[i],Integer.parseInt(filedValList[i]));
            }
        }
    }
    public void editWarrantyClaimCBUField(Map<String, Object> map){
        String id=(String) map.get("id") ;
        String filedName=(String) map.get("filedName");
        String filedVal=(String) map.get("filedVal");
        String filedType=(String) map.get("filedType");
        String[] filedNameList=filedName.split("/");
        String[] filedValList=filedVal.split(Pattern.quote("|&&|"));
        String[] filedTypeList=filedType.split("/");
        for (int i = 0; i < filedNameList.length; i++) {
            //确认类型
            if (filedTypeList[i].equals("str")){
                problemMapp.editWarrantyClaimCBUOneField(id, filedNameList[i],filedValList[i]);
            }
            else if (filedTypeList[i].equals("int")){
                problemMapp.editWarrantyClaimCBUOneFieldInt(id, filedNameList[i],Integer.parseInt(filedValList[i]));
            }
        }
    }
    public void editWarrantyClaimKDField(Map<String, Object> map){
        String id=(String) map.get("id") ;
        String filedName=(String) map.get("filedName");
        String filedVal=(String) map.get("filedVal");
        String filedType=(String) map.get("filedType");
        String[] filedNameList=filedName.split("/");
        String[] filedValList=filedVal.split(Pattern.quote("|&&|"));
        String[] filedTypeList=filedType.split("/");
        for (int i = 0; i < filedNameList.length; i++) {
            //确认类型
            if (filedTypeList[i].equals("str")){
                problemMapp.editWarrantyClaimKDOneField(id, filedNameList[i],filedValList[i]);
            }
            else if (filedTypeList[i].equals("int")){
                problemMapp.editWarrantyClaimKDOneFieldInt(id, filedNameList[i],Integer.parseInt(filedValList[i]));
            }
        }
    }

    //辅助函数，根据id，向后端进行筛选问题库所有的数据
    public problemDataPojo inquiryProblemById(Map<String, Object> map){
        Integer id= Integer.parseInt(map.get("id").toString());
        problemDataPojo pojo=problemMapp.inquiryProblemById(id);
        return pojo;
    }

    //辅助函数，在索赔单中，查询出所有的IPTV问题，通过一个IPTV类型，找到所有不为空的数据
    public List<warrantyClaimsPojo>  inquiryWarrantyClaimOneFiledNoVoid(String filedName){
        List<warrantyClaimsPojo>  pojo=problemMapp.inquiryWarrantyClaimOneFiledNoVoid(filedName);
        return pojo;
    }
    public List<warrantyClaimsCBUPojo>  inquiryWarrantyClaimCBUOneFiledNoVoid(String filedName){
        List<warrantyClaimsCBUPojo>  pojo=problemMapp.inquiryWarrantyClaimCBUOneFiledNoVoid(filedName);
        return pojo;
    }
    public List<warrantyClaimsKDPojo>  inquiryWarrantyClaimKDOneFiledNoVoid(String filedName){
        List<warrantyClaimsKDPojo>  pojo=problemMapp.inquiryWarrantyClaimKDOneFiledNoVoid(filedName);
        return pojo;
    }

    //辅助函数，在索赔单中，查询出用户指定的IPTV类型，指定的月份的数据，采用的是一个字段，等于多个值的方法,值需要用’/‘隔开，查询的都是字符串
    public List<warrantyClaimsPojo> inquiryWarrantyClaimOneFiledSeveral(Map<String, Object> map){
        String filedName=(String) map.get("filedName");
        String filedVal=(String) map.get("filedVal");
        String[] filedValList=filedVal.split("/");
        List<warrantyClaimsPojo> pojos=new ArrayList<>();
        for (int i = 0; i < filedValList.length; i++) {
            List<warrantyClaimsPojo> warrantyClaimsPojoList=problemMapp.inquiryWarrantyClaimOneFiled(filedName,filedValList[i]);
            pojos.addAll(warrantyClaimsPojoList);
        }
        return pojos;
    }
    public List<warrantyClaimsCBUPojo> inquiryWarrantyClaimCBUOneFiledSeveral(Map<String, Object> map){
        String filedName=(String) map.get("filedName");
        String filedVal=(String) map.get("filedVal");
        String[] filedValList=filedVal.split("/");
        List<warrantyClaimsCBUPojo> pojos=new ArrayList<>();
        for (int i = 0; i < filedValList.length; i++) {
            List<warrantyClaimsCBUPojo> warrantyClaimsCBUPojoList=problemMapp.inquiryWarrantyClaimCBUOneFiled(filedName,filedValList[i]);
            pojos.addAll(warrantyClaimsCBUPojoList);
        }
        return pojos;
    }
    public List<warrantyClaimsKDPojo> inquiryWarrantyClaimKDOneFiledSeveral(Map<String, Object> map){
        String filedName=(String) map.get("filedName");
        String filedVal=(String) map.get("filedVal");
        String[] filedValList=filedVal.split("/");
        List<warrantyClaimsKDPojo> pojos=new ArrayList<>();
        for (int i = 0; i < filedValList.length; i++) {
            List<warrantyClaimsKDPojo> warrantyClaimsKDPojoList=problemMapp.inquiryWarrantyClaimKDOneFiled(filedName,filedValList[i]);
            pojos.addAll(warrantyClaimsKDPojoList);
        }
        return pojos;
    }

    //辅助函数，在索赔单中，根据一个字段，查询出这个字段等于某一个值或者多个字的数据，值需要用’/‘隔开，用在查询int格式上
    public List<warrantyClaimsPojo> inquiryWarrantyClaimOneFiledSeveralInt(Map<String, Object> map){
        String filedName=(String) map.get("filedName");
        String filedVal=(String) map.get("filedVal");
        String[] filedValList=filedVal.split("/");
        List<warrantyClaimsPojo> pojos=new ArrayList<>();
        for (int i = 0; i < filedValList.length; i++) {
            List<warrantyClaimsPojo> warrantyClaimsPojoList=problemMapp.inquiryWarrantyClaimOneFiledInt(filedName,Integer.parseInt(filedValList[i]));
            pojos.addAll(warrantyClaimsPojoList);
        }
        return pojos;
    }
    //辅助函数：调取所有的不良品信息
    public List<noGoodPartAssPojo> inquiryNoGoodPartAll(){
        List<noGoodPartAssPojo> pojos=problemMapp.inquiryNoGoodPartAll();
        return pojos;
    }
    //辅助函数：向后端的不合格品库中写入代码，需要写入多个字段
    public void editNoGoodDataField(Map<String, Object> map){
        String id=(String) map.get("id") ;
        String filedName=(String) map.get("filedName");
        String filedVal=(String) map.get("filedVal");
        String filedType=(String) map.get("filedType");
        String[] filedNameList=filedName.split("/");
        String[] filedValList=filedVal.split("/");
        String[] filedTypeList=filedType.split("/");
        for (int i = 0; i < filedNameList.length; i++) {
            //确认类型
            if (filedTypeList[i].equals("str")){
                problemMapp.editNoGoodDataOneField(id, filedNameList[i],filedValList[i]);
            }
        }
    }
    //辅助函数：查询零公里所有的数据
    public List<zeroKilometersPojo> inquiryZeroKilometersAll(){
        List<zeroKilometersPojo> pojos=problemMapp.inquiryZeroKilometersAll();
        return pojos;
    }
    //辅助函数，对零公里的清单中，插入一行数据
    public void inertZeroKilometersOne(zeroKilometersPojo pojo){
        pojo.setEstablishTime(LocalDateTime.now());
        pojo.setUpdateTime(LocalDateTime.now());
        problemMapp.inertZeroKilometersOne(pojo);
    }
    //辅助函数，对零公里清单中（记录的不是零公里，是所有的问题），根据一个字段，查询等于该字段的所有数据,是字符串的
    public List<zeroKilometersPojo> inquiryOneFieldZeroKilometers(Map<String, Object> map){
        String fieldName=(String) map.get("fieldName");
        String fieldVal=(String) map.get("fieldVal");
        List<zeroKilometersPojo> pojos=problemMapp.inquiryOneFieldZeroKilometers(fieldName,fieldVal);
        return pojos;
    }
    //辅助函数，对零公里清单中（记录的不是零公里，是所有的问题），根据id，修改一行的多个字段，每个字段用’/‘隔开，内容同样用’/‘隔开，类型也用’/'隔开
    public void editZeroKilometersField(Map<String, Object> map){
        Integer id=(Integer) map.get("id") ;
        String filedName=(String) map.get("filedName");
        String filedVal=(String) map.get("filedVal");
        String filedType=(String) map.get("filedType");
        String[] filedNameList=filedName.split("\\|\\*\\|");
        String[] filedValList=filedVal.split("\\|\\*\\|");
        String[] filedTypeList=filedType.split("\\|\\*\\|");
        for (int i = 0; i < filedNameList.length; i++) {
            //确认类型
            if (filedTypeList[i].equals("str")){
                problemMapp.editZeroKilometersField(id, filedNameList[i],filedValList[i]);
            }
            else if (filedTypeList[i].equals("int")){
                problemMapp.editZeroKilometersFieldInt(id, filedNameList[i],Integer.parseInt(filedValList[i]));
            }
        }
    }
    //辅助函数：对不良品库中，根据一个字段，查询这个字段等于某一值，可以等于多个值，每个值中间有‘/’隔开；配的是类型；
    public List<noGoodPartAssPojo> inquiryNoGoodFiledSeveral(Map<String, Object> map){
        String filedName=(String) map.get("filedName");
        String filedVal=(String) map.get("filedVal");
        String[] filedValList=filedVal.split("/");
        List<noGoodPartAssPojo> pojos=new ArrayList<>();
        for (int i = 0; i < filedValList.length; i++) {
            List<noGoodPartAssPojo> noGoodPartAssPojoList=problemMapp.inquiryNoGoodFiledSeveral(filedName,filedValList[i]);
            pojos.addAll(noGoodPartAssPojoList);
        }
        return pojos;
    }
    //辅助函数：向后端IPTV报告中筛选数据，可以实现筛选多个等段同时等于多个数值，最多可以为10个。
    public List<iptvReportDataPojo> inquiryIptvReportSeveralField(Map<String, Object> map){
        String fieldName=(String) map.get("fieldName");
        String fieldVal=(String) map.get("fieldVal");
        String[] filedValList=fieldVal.split("/");
        String[] filedNameList=fieldName.split("/");
        List<iptvReportDataPojo> pojos=new ArrayList<>();
        if (filedNameList.length==1){
            pojos=this.problemMapp.inquiryIptvReportField1(filedNameList[0],filedValList[0]);
        }
        else if (filedNameList.length==2){
            pojos=this.problemMapp.inquiryIptvReportField2(filedNameList[0],filedValList[0],filedNameList[1],filedValList[1]);
        }
        else if (filedNameList.length==3){
            pojos=this.problemMapp.inquiryIptvReportField3(filedNameList[0],filedValList[0],filedNameList[1],filedValList[1],filedNameList[2],filedValList[2]);
        }
        else if (filedNameList.length==4){
            pojos=this.problemMapp.inquiryIptvReportField4(filedNameList[0],filedValList[0],filedNameList[1],filedValList[1],filedNameList[2],filedValList[2],filedNameList[3],filedValList[3]);
        }
        return pojos;
    }
    //插入一行IPTV报告，所有的字段都插入
    public void insertOneIptvReport(iptvReportDataPojo pojo){
        pojo.setEstablishTime(LocalDateTime.now());
        pojo.setUpdateTime(LocalDateTime.now());
        problemMapp.insertOneIptvReport(pojo);
    }
    //根据id，修改IptvReport中所有的数据
    public void editOneLineIptvReport(iptvReportDataPojo pojo){
        pojo.setUpdateTime(LocalDateTime.now());
        problemMapp.editOneLineIptvReport(pojo);
    }
    //辅助函数：向后端IPTV报告中查询所有的数据
    public List<iptvReportDataPojo> inquiryIptvReportAll(){
        List<iptvReportDataPojo> pojos=problemMapp.inquiryIptvReportAll();
        return pojos;
    }
    //辅助函数，对零公里清单中（记录的不是零公里，是所有的问题），修改一行所有的数据
    public void editZeroKilometersOneAllById(zeroKilometersPojo pojo){
        pojo.setUpdateTime(LocalDateTime.now());
        problemMapp.editZeroKilometersOneAllById(pojo);
    }
    //辅助函数，对零公里清单中（记录的不是零公里，是所有的问题），根据一个字段，查询等于该字段的所有数据,是整数型的，主要用于int
    public List<zeroKilometersPojo> inquiryOneFieldZeroKilometersInt(Map<String, Object> map){
        String fieldName=(String) map.get("fieldName");
        Integer fieldVal=(Integer) map.get("fieldVal");
        List<zeroKilometersPojo> pojos=problemMapp.inquiryOneFieldZeroKilometersInt(fieldName,fieldVal);
        return pojos;
    }
    //辅助函数，在索赔单中，根据一个字段，查询这个字段的一个范围,用在查询字符串类型的，主要用在了查询文本格式保存的时间上
    public List<warrantyClaimsPojo> inquiryWarrantyClaimOneFiledScope(Map<String, Object> map){
        String filedName = (String) map.get("filedName");
        String minVal =(String) map.get("minVal");
        String maxVal= (String) map.get("maxVal");
        List<warrantyClaimsPojo> pojos = problemMapp.inquiryWarrantyClaimOneFiledScope(filedName,minVal,maxVal);
        return pojos;
    }
    //辅助函数，在不良品记录表中，查询有多少条数据
    public Integer inquiryNoGoodPartTotal(){
        Integer total=problemMapp.inquiryNoGoodPartTotal();
        return total;
    }
    //辅助函数：对不良品筛选，页数、每页最大数，按照输入的字段排倒序进行筛选
    public List<noGoodPartAssPojo> inquiryPaginationNoGoodPart(Map<String, Object> map){
        int pageSize=(int) map.get("pageSize");
        int pageNumber=(int) map.get("pageNumber");
        int startPage = (pageNumber-1)*pageSize;
        //向数据库查询
        List<noGoodPartAssPojo> noGoodPartAssPojoList=problemMapp.inquiryPaginationNoGoodPart(startPage,pageSize);
        return noGoodPartAssPojoList;
    }

    //辅助函数：向后端查询售后索赔单有分析中的总数
    public Integer queryAnalyzingQuantity(){
        Integer total=problemMapp.queryAnalyzingQuantity();
        return total;
    }
    //辅助函数：向后端查询售后索赔单有多少条数据
    public Integer inquiryWarrantyClaimTotal(){
        Integer total=problemMapp.inquiryWarrantyClaimTotal();
        return total;
    }
    //辅助函数：对索赔单（俄动力）筛选，页数、每页最大数，按照输入的字段排倒序进行筛选
    public List<warrantyClaimsPojo> inquiryPaginationWarrantyClaim(Map<String, Object> map){
        int pageSize=(int) map.get("pageSize");
        int pageNumber=(int) map.get("pageNumber");
        int startPage = (pageNumber-1)*pageSize;
        //向数据库查询
        List<warrantyClaimsPojo> warrantyClaimsPojoList=problemMapp.inquiryPaginationWarrantyClaim(startPage,pageSize);
        return warrantyClaimsPojoList;
    }
    //辅助函数：向后端查询CBU售后索赔单有多少条数据
    public Integer inquiryWarrantyClaimCBUTotal(){
        Integer total=problemMapp.inquiryWarrantyClaimCBUTotal();
        return total;
    }
    //辅助函数：对CBU索赔单（俄动力）筛选，页数、每页最大数，按照输入的字段排倒序进行筛选
    public List<warrantyClaimsCBUPojo> inquiryPaginationWarrantyClaimCBU(Map<String, Object> map){
        int pageSize=(int) map.get("pageSize");
        int pageNumber=(int) map.get("pageNumber");
        int startPage = (pageNumber-1)*pageSize;
        //向数据库查询
        List<warrantyClaimsCBUPojo> warrantyClaimsCBUPojoList=problemMapp.inquiryPaginationWarrantyClaimCBU(startPage,pageSize);
        return warrantyClaimsCBUPojoList;
    }
    //辅助函数：向后端查询KD售后索赔单有多少条数据
    public Integer inquiryWarrantyClaimKDTotal(){
        Integer total=problemMapp.inquiryWarrantyClaimKDTotal();
        return total;
    }
    //辅助函数：对KD索赔单（俄动力）筛选，页数、每页最大数，按照输入的字段排倒序进行筛选
    public List<warrantyClaimsKDPojo> inquiryPaginationWarrantyClaimKD(Map<String, Object> map){
        int pageSize=(int) map.get("pageSize");
        int pageNumber=(int) map.get("pageNumber");
        int startPage = (pageNumber-1)*pageSize;
        //向数据库查询
        List<warrantyClaimsKDPojo> warrantyClaimsKDPojoList=problemMapp.inquiryPaginationWarrantyClaimKD(startPage,pageSize);
        return warrantyClaimsKDPojoList;
    }

    //修改售后索赔单中是否返件字段
    public void editOneById(warrantyClaimsPojo pojo){
        pojo.setUpdateTime(LocalDateTime.now());
        problemMapp.editOneById(pojo);
    }
    public void editOneByIdCBU(warrantyClaimsCBUPojo pojo){
        pojo.setUpdate_time(LocalDateTime.now());
        problemMapp.editOneByIdCBU(pojo);
    }
    public void editOneByIdKD(warrantyClaimsKDPojo pojo){
        pojo.setUpdate_time(LocalDateTime.now());
        problemMapp.editOneByIdKD(pojo);
    }

    //修改售后索赔单中是否分析完成字段
    public void editIsAnalyzeEnd(warrantyClaimsPojo pojo){
        pojo.setUpdateTime(LocalDateTime.now());
        problemMapp.editIsAnalyzeEnd(pojo);
    }
    //修改售后索赔单中是否汇报字段
    public void editIsReport(warrantyClaimsPojo pojo){
        pojo.setUpdateTime(LocalDateTime.now());
        problemMapp.editIsReport(pojo);
    }

    //筛选索赔单中多个筛选变量iptv
    public List<warrantyClaimsPojo> filterWarrantyClaims(Map<String, Object> map){
        String selectMoon=(String)map.get("selectMoon");
        String[] selectMoonArray = selectMoon.split("/");
        String iptvType=(String) map.get("iptvType");
        String drawingSelect=(String) map.get("drawingSelect");
        String engineNoSelect=(String) map.get("engineNoSelect");
        String selectDateInterval=(String) map.get("selectDateInterval");
        String isAssessment=(String) map.get("isAssessment");
        Integer startPage1=(Integer) map.get("startPage");
        Integer pageSize=(Integer) map.get("pageSize");
        int startPage = (startPage1-1)*pageSize;
        List<warrantyClaimsPojo> warrantyClaimsPojoList =new ArrayList<>();
        if (iptvType.equals("All Problem")){
            if (selectMoon==null || selectMoon.equals("")){
                warrantyClaimsPojoList = problemMapp.inquiryWarrantyClaimAll();
            }
            else{
                for (int i = 0; i < selectMoonArray.length; i++) {
                    warrantyClaimsPojoList.addAll(problemMapp.filterWarrantyClaimIptv(startPage,pageSize,selectMoonArray[i]));
                }
                if (isAssessment.equals("是")){
                    warrantyClaimsPojoList=warrantyClaimsPojoList.stream()
                            .filter(item -> "是".equals(item.getIsExam()))
                            .collect(Collectors.toList());
                }
                else if (isAssessment.equals("否")){
                    warrantyClaimsPojoList=warrantyClaimsPojoList.stream()
                            .filter(item -> "否".equals(item.getIsExam()))
                            .collect(Collectors.toList());
                }
            }
        }
        else {
            if (selectMoon==null || selectMoon.equals("")){
                if (isAssessment.equals("是")){
                    warrantyClaimsPojoList=problemMapp.filterWarrantyClaimIptvOneIs(iptvType,isAssessment);
                }
                else if (isAssessment.equals("否")){
                    warrantyClaimsPojoList=problemMapp.filterWarrantyClaimIptvOneIs(iptvType,isAssessment);
                }
                else{
                    warrantyClaimsPojoList=problemMapp.filterWarrantyClaimIptvOne(iptvType);
                }
            }
            else {
                for (int i = 0; i < selectMoonArray.length; i++) {
                    warrantyClaimsPojoList.addAll(problemMapp.filterWarrantyClaimIptvValue(iptvType,selectMoonArray[i]));
                }
                if (isAssessment.equals("是")){
                    warrantyClaimsPojoList=warrantyClaimsPojoList.stream()
                            .filter(item -> "是".equals(item.getIsExam()))
                            .collect(Collectors.toList());
                }
                else if (isAssessment.equals("否")){
                    warrantyClaimsPojoList=warrantyClaimsPojoList.stream()
                            .filter(item -> "否".equals(item.getIsExam()))
                            .collect(Collectors.toList());
                }
            }
        }
        int endPage = startPage + pageSize;
        if (endPage>warrantyClaimsPojoList.size()){
            endPage = warrantyClaimsPojoList.size();
        }
        warrantyClaimsPojoList = warrantyClaimsPojoList.subList(startPage, endPage);
        return warrantyClaimsPojoList;
    }
    //筛选索赔单中多个筛选变量的总数iptv
    public Integer filterWarrantyClaimsQuantity(Map<String, Object> map){
        String selectMoon=(String)map.get("selectMoon");
        String[] selectMoonArray = selectMoon.split("/");
        String iptvType=(String) map.get("iptvType");
        String drawingSelect=(String) map.get("drawingSelect");
        String engineNoSelect=(String) map.get("engineNoSelect");
        String selectDateInterval=(String) map.get("selectDateInterval");
        String isAssessment=(String) map.get("isAssessment");
        Integer startPage1=(Integer) map.get("startPage");
        Integer pageSize=(Integer) map.get("pageSize");
        int startPage = (startPage1-1)*pageSize;
        Integer total =0;
        List<warrantyClaimsPojo> warrantyClaimsPojoList =new ArrayList<>();
        if (iptvType.equals("All Problem")){
            if (selectMoon==null || selectMoon.equals("")){
                total=problemMapp.inquiryWarrantyClaimTotal();
            }
            else{
                for (int i = 0; i < selectMoonArray.length; i++) {
                    warrantyClaimsPojoList.addAll(problemMapp.filterWarrantyClaimIptv(startPage,pageSize,selectMoonArray[i]));
                }
                if (isAssessment.equals("是")){
                    warrantyClaimsPojoList=warrantyClaimsPojoList.stream()
                            .filter(item -> "是".equals(item.getIsExam()))
                            .collect(Collectors.toList());
                }
                else if (isAssessment.equals("否")){
                    warrantyClaimsPojoList=warrantyClaimsPojoList.stream()
                            .filter(item -> "否".equals(item.getIsExam()))
                            .collect(Collectors.toList());
                }
                total=warrantyClaimsPojoList.size();
            }
        }
        else {
            if (selectMoon==null || selectMoon.equals("")){
                if (isAssessment.equals("是")){
                    warrantyClaimsPojoList=problemMapp.filterWarrantyClaimIptvOneIs(iptvType,isAssessment);
                }
                else if (isAssessment.equals("否")){
                    warrantyClaimsPojoList=problemMapp.filterWarrantyClaimIptvOneIs(iptvType,isAssessment);
                }
                else{
                    warrantyClaimsPojoList=problemMapp.filterWarrantyClaimIptvOne(iptvType);
                }
            }
            else {
                for (int i = 0; i < selectMoonArray.length; i++) {
                    warrantyClaimsPojoList.addAll(problemMapp.filterWarrantyClaimIptvValue(iptvType,selectMoonArray[i]));
                }
                if (isAssessment.equals("是")){
                    warrantyClaimsPojoList=warrantyClaimsPojoList.stream()
                            .filter(item -> "是".equals(item.getIsExam()))
                            .collect(Collectors.toList());
                }
                else if (isAssessment.equals("否")){
                    warrantyClaimsPojoList=warrantyClaimsPojoList.stream()
                            .filter(item -> "否".equals(item.getIsExam()))
                            .collect(Collectors.toList());
                }
            }
            total=warrantyClaimsPojoList.size();
        }
        return total;
    }

    //筛选索赔单中多个筛选变量Engine
    public List<warrantyClaimsPojo> filterWarrantyClaimsEngine(Map<String, Object> map){
        String engineNoSelect=(String) map.get("engineNoSelect");
        Integer startPage1=(Integer) map.get("startPage");
        Integer pageSize=(Integer) map.get("pageSize");
        int startPage = (startPage1-1)*pageSize;
        List<warrantyClaimsPojo> warrantyClaimsPojoList=problemMapp.filterWarrantyClaimsEngine(engineNoSelect,startPage,pageSize);
        return warrantyClaimsPojoList;
    }
    //筛选索赔单中多个筛选变量的总数Engine
    public Integer filterWarrantyClaimsQuantityEngine(Map<String, Object> map){
        String engineNoSelect=(String) map.get("engineNoSelect");
        Integer total=problemMapp.filterWarrantyClaimsQuantityEngine(engineNoSelect);
        return total;
    }

    //筛选索赔单中多个筛选变量Parts
    public List<warrantyClaimsPojo> filterWarrantyClaimsParts(Map<String, Object> map){
        String drawingSelect=(String) map.get("drawingSelect");
        Integer startPage1=(Integer) map.get("startPage");
        Integer pageSize=(Integer) map.get("pageSize");
        int startPage = (startPage1-1)*pageSize;
        List<warrantyClaimsPojo> warrantyClaimsPojoList=problemMapp.filterWarrantyClaimsParts(drawingSelect,startPage,pageSize);
        return warrantyClaimsPojoList;
    }
    //筛选索赔单中多个筛选变量的总数Parts
    public Integer filterWarrantyClaimsQuantityParts(Map<String, Object> map){
        String drawingSelect=(String) map.get("drawingSelect");
        Integer total=problemMapp.filterWarrantyClaimsQuantityParts(drawingSelect);
        return total;
    }

    //筛选索赔单中多个筛选变量ClaimNumber
    public List<warrantyClaimsPojo> filterWarrantyClaimsClaimNumber(Map<String, Object> map){
        String claimNumber=(String) map.get("claimNumber");
        Integer startPage1=(Integer) map.get("startPage");
        Integer pageSize=(Integer) map.get("pageSize");
        int startPage = (startPage1-1)*pageSize;
        List<warrantyClaimsPojo> warrantyClaimsPojoList=problemMapp.filterWarrantyClaimsClaimNumber(claimNumber,startPage,pageSize);
        return warrantyClaimsPojoList;
    }
    //筛选索赔单中多个筛选变量的总数ClaimNumber
    public Integer filterWarrantyClaimsQuantityClaimNumber(Map<String, Object> map){
        String claimNumber=(String) map.get("claimNumber");
        Integer total=problemMapp.filterWarrantyClaimsQuantityClaimNumber(claimNumber);
        return total;
    }

    //筛选索赔单中多个筛选变量IsAnalyzeEnd
    public List<warrantyClaimsPojo> filterWarrantyClaimsIsAnalyzeEnd(Map<String, Object> map){
        String isAnalyzeEnd=(String) map.get("isAnalyzeEnd");
        Integer startPage1=(Integer) map.get("startPage");
        Integer pageSize=(Integer) map.get("pageSize");
        int startPage = (startPage1-1)*pageSize;
        List<warrantyClaimsPojo> warrantyClaimsPojoList=problemMapp.filterWarrantyClaimsIsAnalyzeEnd(isAnalyzeEnd,startPage,pageSize);
        return warrantyClaimsPojoList;
    }
    //筛选索赔单中多个筛选变量的总数IsAnalyzeEnd
    public Integer filterWarrantyClaimsQuantityIsAnalyzeEnd(Map<String, Object> map){
        String isAnalyzeEnd=(String) map.get("isAnalyzeEnd");
        Integer total=problemMapp.filterWarrantyClaimsQuantityIsAnalyzeEnd(isAnalyzeEnd);
        return total;
    }

    //筛选索赔单中多个筛选变量IsReport
    public List<warrantyClaimsPojo> filterWarrantyClaimsIsReport(Map<String, Object> map){
        String isReport=(String) map.get("isReport");
        Integer startPage1=(Integer) map.get("startPage");
        Integer pageSize=(Integer) map.get("pageSize");
        int startPage = (startPage1-1)*pageSize;
        List<warrantyClaimsPojo> warrantyClaimsPojoList=problemMapp.filterWarrantyClaimsIsReport(isReport,startPage,pageSize);
        return warrantyClaimsPojoList;
    }
    //筛选索赔单中多个筛选变量的总数IsReport
    public Integer filterWarrantyClaimsQuantityIsReport(Map<String, Object> map){
        String isReport=(String) map.get("isReport");
        Integer total=problemMapp.filterWarrantyClaimsQuantityIsReport(isReport);
        return total;
    }

    //在返件清单页面搜索索赔单、CBU、KD中的索赔编号
    public List<warrantyClaimsPojo> searchClaimNumber(Map<String, Object> map){
        String fieldName = (String) map.get("fieldName");
        String fieldValue = (String) map.get("fieldValue");
        List<warrantyClaimsPojo> warrantyClaimsList = problemMapp.searchClaimNumber(fieldName, fieldValue);
        return warrantyClaimsList;
    }
    public List<warrantyClaimsCBUPojo> searchClaimNumberCBU(Map<String, Object> map){
        String fieldName = (String) map.get("fieldName");
        String fieldValue = (String) map.get("fieldValue");
        List<warrantyClaimsCBUPojo> warrantyClaimsList = problemMapp.searchClaimNumberCBU(fieldName, fieldValue);
        return warrantyClaimsList;
    }
    public List<warrantyClaimsKDPojo> searchClaimNumberKD(Map<String, Object> map){
        String fieldName = (String) map.get("fieldName");
        String fieldValue = (String) map.get("fieldValue");
        List<warrantyClaimsKDPojo> warrantyClaimsList = problemMapp.searchClaimNumberKD(fieldName, fieldValue);
        return warrantyClaimsList;
    }

    //查询所有索赔单数据
    public List<warrantyClaimsPojo> queryAll(){
        List<warrantyClaimsPojo> pojoList = problemMapp.queryAll();
        return pojoList;
    }
    //查询所有索赔单数据CBU
    public List<warrantyClaimsCBUPojo> queryAllCBU(){
        List<warrantyClaimsCBUPojo> pojoList = problemMapp.queryAllCBU();
        return pojoList;
    }
    //查询所有索赔单数据KD
    public List<warrantyClaimsKDPojo> queryAllKD(){
        List<warrantyClaimsKDPojo> pojoList = problemMapp.queryAllKD();
        return pojoList;
    }

    //查找问题清单对应索赔单中的零部件编号
    public List<problemDataPojo> inquiryProblemByDrawingPart(String mainPartCode){
        return problemMapp.inquiryProblemByDrawingPart(mainPartCode);
    }

    //删除问题清单问题明细中某条关联索赔单的数据（修改两个字段）
    public void editTwoFields(Map<String, Object> map){
        Integer id = (Integer) map.get("id");
        String fieldName1 = (String) map.get("fieldName1");
        String fieldValue1 = (String) map.get("fieldValue1");
        String fieldName2 = (String) map.get("fieldName2");
        String fieldValue2 = (String) map.get("fieldValue2");
        problemMapp.editTwoFields(id,fieldName1,fieldValue1,fieldName2,fieldValue2);
    }
}
